//
//  HomeRequest.m
//  SleepingElf
//
//  Created by karong on 2023/10/9.
//

#import "HomeRequest.h"
#import "HomeModel.h"

@implementation HomeRequest
+ (void)loadHomeListWithSuccess:(AnyObjectBlock)success failed:(Failure)failed {
    HomeModel * homeModel = [HomeModel new];
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        [HomeRequest loadHomeBannerWithSuccess:^(NSArray *array) {
            homeModel.bannerArray = array;
            dispatch_semaphore_signal(semaphore);
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_async(group, queue, ^{
        [HomeRequest loadHomeTabWithSuccess:^(NSArray *array) {
            homeModel.tabArray = array;
            dispatch_semaphore_signal(semaphore);
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_async(group, queue, ^{
        [HomeRequest loadHomeFeaturedWithSuccess:^(NSArray *array) {
            homeModel.featuredArray = array;
            dispatch_semaphore_signal(semaphore);
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_async(group, queue, ^{
        [HomeRequest loadHomeClassifyWithParent_id:1 success:^(NSArray *array) {
            homeModel.classifyArray = array;
            if (array.count > 0) {
                HomeClassify * homeClassify = array.firstObject;
                [HomeRequest loadHomePlayClasslistWithClassify_id:homeClassify.id page:1 pageSize:3 success:^(NSArray *array) {
                    homeModel.classifyListArray = array;
                    dispatch_semaphore_signal(semaphore);
                } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
                    dispatch_semaphore_signal(semaphore);
                }];
            }
            else {
                dispatch_semaphore_signal(semaphore);
            }
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_async(group, queue, ^{
        [HomeRequest loadHomeLullabyWithPage:1 success:^(NSArray *array) {
            homeModel.lullabyArray = array;
            dispatch_semaphore_signal(semaphore);
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_async(group, queue, ^{
        [HomeRequest loadHomeSleepAndRelaxWithPage:1 pageSize:10 success:^(NSArray *array) {
            homeModel.exclusiveArray = array;
            dispatch_semaphore_signal(semaphore);
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_notify(group, queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            success(homeModel);
        });
    });
}
///banner
+ (void)loadHomeBannerWithSuccess:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_BANNER;
    NSDictionary * parameters = @{};
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [HomeBanner mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

///tab
+ (void)loadHomeTabWithSuccess:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_TAB;
    NSDictionary * parameters = @{};
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [HomeTab mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

///精选白噪音
+ (void)loadHomeFeaturedWithSuccess:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_FEATURED;
    NSDictionary * parameters = @{@"page":@(1),
                                  @"pagesize":@(10)};
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [MusicModel mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

///分类
+ (void)loadHomeClassifyWithParent_id:(NSInteger)parent_id success:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_CLASSIFY;
    NSDictionary * parameters = @{@"parent_id":@(parent_id),
                                  @"page":@(1),
                                  @"pagesize":@(150)
    };
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [HomeClassify mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

///分类列表
+ (void)loadHomePlayClasslistWithClassify_id:(NSInteger)classify_id page:(NSInteger)page pageSize:(NSInteger)pageSize success:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_CLASSIFY_LIST;
    NSMutableDictionary * parameters = [NSMutableDictionary new];
    if (classify_id > 0) {
        parameters[@"classify_id"] = @(classify_id);
    }
    parameters[@"page"] = @(page);
    parameters[@"pagesize"] = @(pageSize);
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [MusicModel mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

///催眠曲
+ (void)loadHomeLullabyWithPage:(NSInteger)page success:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_LULLABY;
    NSDictionary * parameters = @{@"page":@(page),
                                  @"pagesize":@(10)};
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [MusicModel mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

///助眠放松
+ (void)loadHomeSleepAndRelaxWithPage:(NSInteger)page pageSize:(NSInteger)pageSize success:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_SLEEP_RELAX;
    NSMutableDictionary * parameters = [NSMutableDictionary new];
    parameters[@"page"] = @(page);
    parameters[@"pagesize"] = @(pageSize);
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [MusicModel mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}
@end
